<?php
  require '../../include/library.inc.php';
  require '../../include/admin_login.inc.php';
    
  $catalog_id = intval($_GET['catalog_id']);

  if($catalog_id) {
	  
	$sql = "SELECT
					order_number
					
				  FROM
					catalog
				WHERE 
				 catalog_id = '$catalog_id'
				";
	$row = mysql_fetch_assoc(query($sql));
	$order_number = $row['order_number'];
		
	$sql = "UPDATE
              catalog
            SET
              order_number = order_number - 1
            WHERE
              order_number > $order_number
              ";
    query($sql);
	
		
		//Премахваме записа от основната таблица
	$sql = "DELETE FROM 
               catalog
            WHERE
               catalog_id = '$catalog_id'
             ";
    query($sql);
	
		
		
	
			$sql = "SELECT
					image_id,
					image
					
				  FROM
					catalog_image
				WHERE 
				 catalog_id = '$catalog_id'
				";
		
			$result = query($sql);
		
            while ($row = mysql_fetch_object($result)) {
    
	
	
	
	unlink('../catalogimage/'.$catalog_id.'/'.$row->image);
	unlink('../catalogimage/'.$catalog_id.'/th_'.$row->image);
			}
	$sql = "DELETE FROM 
               catalog_image
            WHERE 
				 catalog_id = '$catalog_id'
             ";
    query($sql);
	//unlink('../catalogimage/'.$catalog_id.'/');
	
	$_SESSION['message'] = 'Артикула е изтрит успешно!';
	
  }
  
  redirect("catalog.php");
 
?>